package com.example.startapp.activity;


import android.content.Intent;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.startapp.R;
import com.example.startapp.fragment.CategoryFragment;
import com.example.startapp.fragment.HomeFragment;
import com.example.startapp.fragment.MineFragment;
import com.example.startapp.fragment.ShopCarFragment;

/**
 * 主界面fragment
 */
public class MainActivity extends AppCompatActivity {
    private FrameLayout frameLayout;
    HomeFragment homeFragment;
    MineFragment mineFragment;
    ShopCarFragment shopCarFragment;
    CategoryFragment categoryFragment;
    RadioGroup rg;
    FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        frameLayout = findViewById(R.id.f1);
        rg = findViewById(R.id.rg);
        getFragment();
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.home:
                        hideFragment(shopCarFragment);
                        hideFragment(mineFragment);
                        hideFragment(categoryFragment);
                        showFragment(homeFragment);

                        break;
                    case R.id.category:
                        hideFragment(shopCarFragment);
                        hideFragment(mineFragment);
                        hideFragment(homeFragment);
                        showFragment(categoryFragment);
                        break;
                    case R.id.shopcar:
                        hideFragment(categoryFragment);
                        hideFragment(mineFragment);
                        hideFragment(homeFragment);
                        showFragment(shopCarFragment);
                        break;
                    case R.id.mine:
                        hideFragment(shopCarFragment);
                        hideFragment(categoryFragment);
                        hideFragment(homeFragment);
                        showFragment(mineFragment);
                        break;

                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent=getIntent();
        String fragNum=intent.getStringExtra("id");
        if (fragNum!=null){
            switch (fragNum){
                case "1":
                   FragmentTransaction fragmentTransaction1=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction1.replace(R.id.f1,new HomeFragment());
                    fragmentTransaction1.commit();
                    break;
                case "4":
                    /*
                    注册成功跳转
                    并且
                    传值更新个人信息
                     */
//                    MineFragment mineFragment=new MineFragment();
//                    Bundle bundle_message=new Bundle();
//                    bundle_message.putBoolean("success",true);
//                    FragmentManager fm=getSupportFragmentManager();

                    //FragmentTransaction ft=fm.beginTransaction();
//                    ft.add(R.id.f1,MineFragment,"MineFragment");
//                    ft.commit();

                    FragmentTransaction fragmentTransaction4=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction4.replace(R.id.f1,new MineFragment());
                    fragmentTransaction4.commit();
                    break;




            }


        }
    }

    private void getFragment() {
        homeFragment = new HomeFragment();
        shopCarFragment = new ShopCarFragment();
        categoryFragment = new CategoryFragment();
        mineFragment = new MineFragment();

        addFragment(homeFragment);
        addFragment(shopCarFragment);
        addFragment(mineFragment);
        addFragment(categoryFragment);

        hideFragment(homeFragment);
        hideFragment(mineFragment);
        hideFragment(categoryFragment);
        hideFragment(shopCarFragment);
        showFragment(homeFragment);


    }

    private void addFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
         fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.f1, fragment);
        fragmentTransaction.commit();

    }

    private void showFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().show(fragment).commit();

    }

    private void hideFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().hide(fragment).commit();
    }
}


